﻿using System.IO;
using System.Linq;
using GlobalizationTools.Model;

namespace GlobalizationTools.ViewModel
{
    public class TreeViewProjectViewModel: TreeViewItemViewModel
    {
        Project _project;

        public TreeViewProjectViewModel(TreeViewSolutionViewModel parent, Project project) :
            base(parent, true, true)
        {
            _project = project;
            _name = Path.GetFileNameWithoutExtension(_project.FilePath);
        }

        public TreeViewProjectViewModel(TreeViewRootViewModel parent, Project project) :
            base(parent, true, true)
        {
            _project = project;
            _name = Path.GetFileNameWithoutExtension(_project.FilePath);
        }

        protected override void LoadChildrenInternal()
        {
            _project.LoadProject();
            foreach (Resource resource in _project.Resources.OrderByDescending(T => T.IsRootResource).ThenBy(T => T.Include))
            {
                TreeViewItemViewModel childParent = this;
                if (resource.Include.Contains('\\')) {
                    childParent = GetChildParentFolder(resource, childParent);
                }
                if (!resource.IsRootResource)
                {
                    childParent = GetParentResource(resource, childParent);
                }
                TreeViewResourceViewModel model = new TreeViewResourceViewModel(childParent, resource);
                childParent.Children.Add(model);
            }
        }

        private TreeViewItemViewModel GetParentResource(Resource resource, TreeViewItemViewModel parent)
        {
            string name = resource.Include;
            name = Path.GetFileNameWithoutExtension(Path.GetFileNameWithoutExtension(name));
            var list = parent.Children.Where(T => T is TreeViewResourceViewModel && T.Name.StartsWith(name));
            if (list.Count() == 1)
            {
                parent = list.Single();
            }
            return parent;
        }

        private TreeViewItemViewModel GetChildParentFolder(Resource resource, TreeViewItemViewModel parent)
        {
            string name = resource.Include;
            if (parent is TreeViewFolderViewModel)
            {
                TreeViewFolderViewModel folder = (TreeViewFolderViewModel)parent;
                name = name.Substring(folder.GetHierarchyPath().Length);
            }
            if (name.Contains('\\'))
            {
                name = name.Remove(name.IndexOf('\\'));
                var list = parent.Children.Where(T => T is TreeViewFolderViewModel && T.Name == name);
                if (list.Count() == 1)
                {
                    parent = list.Single();
                }
                else
                {
                    TreeViewFolderViewModel newFolder = new TreeViewFolderViewModel(name, parent);
                    parent.Children.Add(newFolder);
                    parent = newFolder;
                }
                parent = GetChildParentFolder(resource, parent);
            }
            return parent;
        }

        public Project Project
        {
            get { return _project; }
        }


    }
}
